//
//  XMFeedbackViewController.m
//  XMJJ_zml
//
//  Created by 丁浩 on 2025/5/22.
//

#import "XMFeedbackViewController.h"
#import "XMFeedbackView.h"
#import "XMNetwork+mine.h"
#import "XMNetwork+find.h"
#import <Photos/Photos.h>
#import "XMImgModel.h"

@interface XMFeedbackViewController ()

@end

@implementation XMFeedbackViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) {
        
    }];
    [self creatUI];
}

- (void)creatUI {
    XMFeedbackView *feedbackView = [[XMFeedbackView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:feedbackView];
    XMWeakSelf;
    feedbackView.submitBlock = ^(NSString * _Nonnull selection,
                                 NSString * _Nonnull contentStr,
                                 NSArray * _Nonnull imgArr) {
        [weakSelf imgUploadWithImgArr:imgArr
                              content:contentStr
                           selectTion:selection];
    };
}

- (void)imgUploadWithImgArr:(NSArray *)imgArr
                    content:(NSString *)contentStr
                 selectTion:(NSString *)selectTion {
    XMWeakSelf;
    [_Network imgUploadWithImg:imgArr
                     directory:@"反馈"
                       success:^(id  _Nonnull responseObject) {
        NSArray *imgModelArr = [NSArray yy_modelArrayWithClass:[XMImgModel class] json:[responseObject safeArrayForKey:@"data"]];
        NSMutableArray *imgUrlArr = [NSMutableArray array];
        for (int i = 0; i < imgModelArr.count; i ++) {
            XMImgModel *imgModel = imgModelArr[i];
            [imgUrlArr addObject:imgModel.url];
        }
        [weakSelf feedbackSubmitWithSelection:selectTion
                                      content:contentStr
                                         imgs:[imgUrlArr componentsJoinedByString:@","]];
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

- (void)feedbackSubmitWithSelection:(NSString *)selection
                            content:(NSString *)content
                               imgs:(NSString *)imgsStr {
    XMWeakSelf;
    [_Network submitFeedbackWithSelection:selection
                                  content:content
                                     imgs:imgsStr
                                  success:^(id  _Nonnull responseObject) {
        [MBProgressHUD showSuccess:@"提交成功"];
        [weakSelf.navigationController popViewControllerAnimated:YES];
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
